package com.example.hospital.api.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.controller.from.deleteDoctorByIdsFrom;
import com.example.hospital.api.db.dao.DoctorDao;
import com.example.hospital.api.db.dao.MedicalDeptSubAndDoctorDao;
import com.example.hospital.api.db.pojo.DoctorEntity;
import com.example.hospital.api.db.pojo.MedicalDeptSubAndDoctorEntity;
import com.example.hospital.api.exception.HospitalException;
import com.example.hospital.api.service.DoctorService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @FileName DoctorServiceImpl
 * @folder
 * @Description
 * @Author yf
 * @return
 * @date 2024-08-19
 **/
@Service
@Slf4j
public class DoctorServiceImpl implements DoctorService {
    @Resource
    private DoctorDao doctorDao;
    @Resource
    private MedicalDeptSubAndDoctorDao medicalDeptSubAndDoctorDao;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;
    /**
    *
    * @folder 分页查询医生
    * @Param: [param]
    * @Author: yf
    * @return: com.example.hospital.api.common.PageUtils
    */
    @Override
    public PageUtils searchByPage(Map param) {
        ArrayList<HashMap> list = null;
        long count = doctorDao.searchCount(param);
        if(count > 0){
            list = doctorDao.searchByPage(param);
        }else {
            list = new ArrayList<>();
        }
        int page = MapUtil.getInt(param, "page");
        int length = MapUtil.getInt(param, "length");
        PageUtils pageUtils = new PageUtils(list, count, page, length);
        return pageUtils;
    }
    /**
    *
    * @folder 查询医生内容
    * @Param: [id]
    * @Author: yf
    * @return: java.util.HashMap
    */
    public HashMap searchContent(int id){
        HashMap map = doctorDao.searchContent(id);
        JSONArray tag = JSONUtil.parseArray(MapUtil.getStr(map, "tag"));
        map.replace("tag", tag);
        return map;
    }

    /**
     *
     * @folder 更新医生头像
     * @Param: [param]
     * @Author: yf
     * @return: void
     */
    @Override
    @Transactional
    public void updatePhoto(MultipartFile file, Integer doctorId) {
        try{
            String filename = "doctor-" + doctorId + ".jpg";
            //在minio中保存医生照片
            MinioClient client = new MinioClient.Builder().endpoint(endpoint)
                    .credentials(accessKey, secretKey).build();

            client.putObject(PutObjectArgs.builder().bucket("hospital")
                    .object("doctor/" + filename)
                    .stream(file.getInputStream(), -1,5 * 1024 * 1024)
                    .contentType("image/jpeg").build());
            //更新医生表photo字段
            doctorDao.updatePhoto(new HashMap(){{
                put("id",doctorId);
                put("photo","/doctor/" + filename);
            }});
        }catch(Exception e){
            log.error("更新医生头像失败",e);
            throw new HospitalException("更新医生头像失败");
        }
    }
    /**
    *
    * @folder 插入医生信息
    * @Param: [entity]
    * @Author: yf
    * @return: void
    */
    @Override
    public void insert(Map param) {
        DoctorEntity doctorEntity = BeanUtil.toBean(param, DoctorEntity.class);
        doctorDao.insert(doctorEntity);
        String uuid = doctorEntity.getUuid();
        Integer doctorId = doctorDao.searchIdByUuid(uuid);
        Integer subId = MapUtil.getInt(param, "subId");
        MedicalDeptSubAndDoctorEntity doctorEntity2 = new MedicalDeptSubAndDoctorEntity();
        doctorEntity2.setDeptSubId(subId);
        doctorEntity2.setDoctorId(doctorId);
        medicalDeptSubAndDoctorDao.insert(doctorEntity2);
    }

    /**
     *
     * @folder 根据id查询医生信息
     * @Param: [id]
     * @Author: yf
     * @return: java.util.HashMap
     */
    public HashMap searchById(int id) {
        HashMap map = doctorDao.searchById(id);
        String tag = MapUtil.getStr(map, "tag");
        JSONArray array = JSONUtil.parseArray(tag);
        map.replace("tag", array);
        return map;
    }

    /**
     *
     * @folder 更新医生信息
     * @Param: [param]
     * @Author: yf
     * @return: void
     */
    @Transactional
    public void update(Map param) {
        doctorDao.update(param);
        param = MapUtil.renameKey(param, "id", "doctorId");
        medicalDeptSubAndDoctorDao.updateDoctorSubDept(param);
    }
    /**
    *
    * @folder 删除医生信息
    * @Param: [form]
    * @Author: yf
    * @return: void
    */

    @Override
    @Transactional
    public void deleteDoctorByIds(Integer[] ids) {
        doctorDao.deleteDoctorByIds(ids);
    }
}
